package com.ymatou.iapi.prodstock.testcase;

import com.ymatou.iapi.liveseller.parameter.ActivityProductDto;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.LiveActivityDto;
import com.ymatou.iapi.prodstock.parameter.FreezeAllocateStockBean;
import com.ymatou.iapi.prodstock.parameter.FreezeCatalogInfo;
import com.ymatou.iapi.prodstock.parameter.FreezeProductResult;
import com.ymatou.iapi.prodstock.parameter.UnfreezeAllocateStockBean;
import com.ymatou.iapi.prodstock.service.UnfreezeAllocateStockCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.database.RedisDriver;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.ProdStockCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.mapping.YmtSecKillStockConfigMapper;
import com.ymttest.database.mapping.YmtSecKillStockConfigSqlProvider;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtSecKillStockConfigExample;
import com.ymttest.database.redis.RedisUtils;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductFreezeStockWapper;
import com.ymttest.database.sqlwapper.YmtSecKillStockConfigWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*解冻库存
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UnfreezeAllocateStock{
	private static UnfreezeAllocateStockBean unfreezeallocatestockBean;
	private static UnfreezeAllocateStockCall unfreezeallocatestockCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static ArrayList<FreezeCatalogInfo> FreezeCatalogList;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("解冻库存");
	}

	@Before
	public void caseUp() {
		unfreezeallocatestockBean=new UnfreezeAllocateStockBean();
		unfreezeallocatestockCall=new UnfreezeAllocateStockCall();
		FreezeCatalogList = new ArrayList<FreezeCatalogInfo>();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(unfreezeallocatestockCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnfreezeAllocateStock_001() {
		Logger.start(true,"无冻结商品解冻库存");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			String freezeId = "JD18628";
			unfreezeallocatestockBean.setActivityId(activityId);
			unfreezeallocatestockBean.setFreezeId(freezeId);
			unfreezeallocatestockBean.setIsFbxProduct(false);
			unfreezeallocatestockBean.setProductInActivityId(Integer.valueOf(aids.get(0)));
			unfreezeallocatestockBean.setProductID(productId);
			unfreezeallocatestockBean.setStoreType(-1);
			unfreezeallocatestockBean.setUserId(sellerId);
			unfreezeallocatestockBean.setOperateType(2);
			FreezeCatalogInfo freezeCatalogInfo = new FreezeCatalogInfo();
			freezeCatalogInfo.setCatalogId(catalogs.get(0));
			freezeCatalogInfo.setFreezeNum(100);
			freezeCatalogInfo.setSku(null);
			FreezeCatalogList.add(freezeCatalogInfo);
			unfreezeallocatestockBean.setFreezeCatalogList(FreezeCatalogList);
			unfreezeallocatestockCall.setData(unfreezeallocatestockBean);
			unfreezeallocatestockCall.callService();
			String Code = unfreezeallocatestockCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnfreezeAllocateStock_002() {
		Logger.start(true,"解冻冻结商品，不同规格，OperateType=3");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			//冻结库存
			String freezeId = "JD168188";
			Integer firstFreezeNum1 = 500;
			FreezeAllocateStockBean initFreezeAllocateStockBean1 = ProdStockCallService.initFreezeAllocateStockBean(activityId, Integer.valueOf(aids.get(0)), freezeId,productId, sellerId, catalogs.get(0), firstFreezeNum1);
			Integer firstFreezeNum2 = 600;
			FreezeAllocateStockBean initFreezeAllocateStockBean2 = ProdStockCallService.initFreezeAllocateStockBean(activityId, Integer.valueOf(aids.get(0)), freezeId,productId, sellerId, catalogs.get(1), firstFreezeNum2);
			ProdStockCallService.FreezeAllocateStock(initFreezeAllocateStockBean1);
			ProdStockCallService.FreezeAllocateStock(initFreezeAllocateStockBean2);
			
			unfreezeallocatestockBean.setActivityId(activityId);
			unfreezeallocatestockBean.setFreezeId(freezeId);
			unfreezeallocatestockBean.setIsFbxProduct(false);
			unfreezeallocatestockBean.setProductInActivityId(Integer.valueOf(aids.get(0)));
			unfreezeallocatestockBean.setProductID(productId);
			unfreezeallocatestockBean.setStoreType(-1);
			unfreezeallocatestockBean.setUserId(sellerId);
			unfreezeallocatestockBean.setOperateType(3);
			FreezeCatalogInfo freezeCatalogInfo1 = new FreezeCatalogInfo();
			freezeCatalogInfo1.setCatalogId(catalogs.get(0));
			freezeCatalogInfo1.setFreezeNum(100);
			freezeCatalogInfo1.setSku(null);
			FreezeCatalogInfo freezeCatalogInfo2 = new FreezeCatalogInfo();
			freezeCatalogInfo2.setCatalogId(catalogs.get(1));
			freezeCatalogInfo2.setFreezeNum(300);
			freezeCatalogInfo2.setSku(null);
			FreezeCatalogList.add(freezeCatalogInfo1);
			FreezeCatalogList.add(freezeCatalogInfo2);
			unfreezeallocatestockBean.setFreezeCatalogList(FreezeCatalogList);
			unfreezeallocatestockCall.setData(unfreezeallocatestockBean);
			unfreezeallocatestockCall.callService();
			
			String Code = unfreezeallocatestockCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			FreezeProductResult freezeProductResult1 = unfreezeallocatestockCall.getResults().get(0);
			Logger.verifyEquals(activityId, freezeProductResult1.getActivityId(), "验证活动编号");
			Logger.verifyEquals(productId, freezeProductResult1.getProductId(), "验证商品编号");
			Logger.verifyEquals(catalogs.get(0), freezeProductResult1.getCatalogId(), "验证规格编号");
			Logger.verifyEquals(freezeId, freezeProductResult1.getFreezeId(), "验证冻结编号");
			Logger.verifyEquals(aids.get(0), freezeProductResult1.getProductInActivityId().toString(), "验证商品在活动中的编号");
		
			FreezeProductResult freezeProductResult2 = unfreezeallocatestockCall.getResults().get(1);
			Logger.verifyEquals(activityId, freezeProductResult2.getActivityId(), "验证活动编号");
			Logger.verifyEquals(productId, freezeProductResult2.getProductId(), "验证商品编号");
			Logger.verifyEquals(catalogs.get(1), freezeProductResult2.getCatalogId(), "验证规格编号");
			Logger.verifyEquals(freezeId, freezeProductResult2.getFreezeId(), "验证冻结编号");
			Logger.verifyEquals(aids.get(0), freezeProductResult2.getProductInActivityId().toString(), "验证商品在活动中的编号");
		
			//冻结库存
			Integer freezeStockNum1 = new YmtProductFreezeStockWapper().selectByProductId(productId).get(0).getFreezeNum();
			Logger.debug(freezeStockNum1);
			Integer freezeStockNum2 = new YmtProductFreezeStockWapper().selectByProductId(productId).get(1).getFreezeNum();
			Logger.debug(freezeStockNum2);
			//规格库存
			Integer catalogNum1 = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(0)).getiNum();
			Integer catalogNum2 = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(1)).getiNum();
			//活动库存
			Integer activityknum1 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Integer activityknum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(1)).getIstocknum();
			
			Logger.verifyEquals(0,freezeStockNum1, "验证冻结表库存");
			Logger.verifyEquals(0,freezeStockNum2, "验证冻结表库存");
			
			Logger.verifyEquals(catalogNum-activityknum1, catalogNum1, "验证规格库存");
			Logger.verifyEquals(catalogNum-activityknum2, catalogNum2, "验证规格库存");
			
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnfreezeAllocateStock_003() {
		Logger.start(true,"解冻冻结商品，不同规格，OperateType=2");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			//初始化活动库存
			Integer anum1 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Integer anum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(1)).getIstocknum();
			//冻结库存
			String freezeId = "JD168188";
			Integer firstFreezeNum1 = 500;
			FreezeAllocateStockBean initFreezeAllocateStockBean1 = ProdStockCallService.initFreezeAllocateStockBean(activityId, Integer.valueOf(aids.get(0)), freezeId,productId, sellerId, catalogs.get(0), firstFreezeNum1);
			Integer firstFreezeNum2 = 600;
			FreezeAllocateStockBean initFreezeAllocateStockBean2 = ProdStockCallService.initFreezeAllocateStockBean(activityId, Integer.valueOf(aids.get(0)), freezeId,productId, sellerId, catalogs.get(1), firstFreezeNum2);
			ProdStockCallService.FreezeAllocateStock(initFreezeAllocateStockBean1);
			ProdStockCallService.FreezeAllocateStock(initFreezeAllocateStockBean2);
			
			unfreezeallocatestockBean.setActivityId(activityId);
			unfreezeallocatestockBean.setFreezeId(freezeId);
			unfreezeallocatestockBean.setIsFbxProduct(false);
			unfreezeallocatestockBean.setProductInActivityId(Integer.valueOf(aids.get(0)));
			unfreezeallocatestockBean.setProductID(productId);
			unfreezeallocatestockBean.setStoreType(-1);
			unfreezeallocatestockBean.setUserId(sellerId);
			unfreezeallocatestockBean.setOperateType(2);
			FreezeCatalogInfo freezeCatalogInfo1 = new FreezeCatalogInfo();
			freezeCatalogInfo1.setCatalogId(catalogs.get(0));
			freezeCatalogInfo1.setFreezeNum(100);
			freezeCatalogInfo1.setSku(null);
			FreezeCatalogInfo freezeCatalogInfo2 = new FreezeCatalogInfo();
			freezeCatalogInfo2.setCatalogId(catalogs.get(1));
			freezeCatalogInfo2.setFreezeNum(300);
			freezeCatalogInfo2.setSku(null);
			FreezeCatalogList.add(freezeCatalogInfo1);
			FreezeCatalogList.add(freezeCatalogInfo2);
			unfreezeallocatestockBean.setFreezeCatalogList(FreezeCatalogList);
			unfreezeallocatestockCall.setData(unfreezeallocatestockBean);
			unfreezeallocatestockCall.callService();
			
			String Code = unfreezeallocatestockCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			FreezeProductResult freezeProductResult1 = unfreezeallocatestockCall.getResults().get(0);
			Logger.verifyEquals(activityId, freezeProductResult1.getActivityId(), "验证活动编号");
			Logger.verifyEquals(productId, freezeProductResult1.getProductId(), "验证商品编号");
			Logger.verifyEquals(catalogs.get(0), freezeProductResult1.getCatalogId(), "验证规格编号");
			Logger.verifyEquals(freezeId, freezeProductResult1.getFreezeId(), "验证冻结编号");
			Logger.verifyEquals(aids.get(0), freezeProductResult1.getProductInActivityId().toString(), "验证商品在活动中的编号");
		
			FreezeProductResult freezeProductResult2 = unfreezeallocatestockCall.getResults().get(1);
			Logger.verifyEquals(activityId, freezeProductResult2.getActivityId(), "验证活动编号");
			Logger.verifyEquals(productId, freezeProductResult2.getProductId(), "验证商品编号");
			Logger.verifyEquals(catalogs.get(1), freezeProductResult2.getCatalogId(), "验证规格编号");
			Logger.verifyEquals(freezeId, freezeProductResult2.getFreezeId(), "验证冻结编号");
			Logger.verifyEquals(aids.get(0), freezeProductResult2.getProductInActivityId().toString(), "验证商品在活动中的编号");
		
			//冻结库存
			Integer freezeStockNum1 = new YmtProductFreezeStockWapper().selectByProductId(productId).get(0).getFreezeNum();
			Integer freezeStockNum2 = new YmtProductFreezeStockWapper().selectByProductId(productId).get(1).getFreezeNum();
			//规格库存
			Integer catalogNum1 = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(0)).getiNum();
			Integer catalogNum2 = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(1)).getiNum();
			//活动库存
			Integer activityknum1 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Integer activityknum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(1)).getIstocknum();
			
			Logger.verifyEquals(0,freezeStockNum1, "验证冻结表库存");
			Logger.verifyEquals(0,freezeStockNum2, "验证冻结表库存");
			
			Logger.verifyEquals(catalogNum-anum1-firstFreezeNum1, catalogNum1, "验证规格库存");
			Logger.verifyEquals(catalogNum-anum2-firstFreezeNum2, catalogNum2, "验证规格库存");
			
			Logger.verifyEquals(firstFreezeNum1+anum1, activityknum1, "验证活动库存");
			Logger.verifyEquals(firstFreezeNum2+anum2, activityknum2, "验证活动库存");
			
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnfreezeAllocateStock_005() {
		Logger.start(true,"解冻冻结商品,解冻参数未负数");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			//初始化活动库存
			Integer anum = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			//冻结库存
			String freezeId = "JD168188";
			Integer firstFreezeNum = 500;
			FreezeAllocateStockBean initFreezeAllocateStockBean = ProdStockCallService.initFreezeAllocateStockBean(activityId, Integer.valueOf(aids.get(0)), freezeId,productId, sellerId, catalogs.get(0), firstFreezeNum);
			ProdStockCallService.FreezeAllocateStock(initFreezeAllocateStockBean);
			
			unfreezeallocatestockBean.setActivityId(activityId);
			unfreezeallocatestockBean.setFreezeId(freezeId);
			unfreezeallocatestockBean.setIsFbxProduct(false);
			unfreezeallocatestockBean.setProductInActivityId(Integer.valueOf(aids.get(0)));
			unfreezeallocatestockBean.setProductID(productId);
			unfreezeallocatestockBean.setStoreType(-1);
			unfreezeallocatestockBean.setUserId(sellerId);
			//unfreezeallocatestockBean.setOperateType(2);
			FreezeCatalogInfo freezeCatalogInfo = new FreezeCatalogInfo();
			freezeCatalogInfo.setCatalogId(catalogs.get(0));
			freezeCatalogInfo.setFreezeNum(-100);
			freezeCatalogInfo.setSku(null);
			FreezeCatalogList.add(freezeCatalogInfo);
			unfreezeallocatestockBean.setFreezeCatalogList(FreezeCatalogList);
			unfreezeallocatestockCall.setData(unfreezeallocatestockBean);
			unfreezeallocatestockCall.callService();
			
			String Code = unfreezeallocatestockCall.getString("Code");
			Logger.verifyEquals("201", Code, "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnfreezeAllocateStock_006() {
		Logger.start(true,"无冻结商品解冻库存");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			String freezeId = "JD18628";
			unfreezeallocatestockBean.setActivityId(activityId);
			unfreezeallocatestockBean.setFreezeId(freezeId);
			unfreezeallocatestockBean.setIsFbxProduct(false);
			unfreezeallocatestockBean.setProductInActivityId(Integer.valueOf(aids.get(0)));
			unfreezeallocatestockBean.setProductID(productId);
			unfreezeallocatestockBean.setStoreType(-1);
			unfreezeallocatestockBean.setUserId(sellerId);
			unfreezeallocatestockBean.setOperateType(2);
			FreezeCatalogInfo freezeCatalogInfo = new FreezeCatalogInfo();
			freezeCatalogInfo.setCatalogId(catalogs.get(0));
			freezeCatalogInfo.setFreezeNum(100);
			freezeCatalogInfo.setSku(null);
			FreezeCatalogList.add(freezeCatalogInfo);
			unfreezeallocatestockBean.setFreezeCatalogList(FreezeCatalogList);
			unfreezeallocatestockCall.setData(unfreezeallocatestockBean);
			unfreezeallocatestockCall.callService();
			String Code = unfreezeallocatestockCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			
			
			
			Integer pia = unfreezeallocatestockCall.getResults().get(0).getProductInActivityId();
			String catalogId = unfreezeallocatestockCall.getResults().get(0).getCatalogId();
			System.out.println(EnvSetup.getSystemUrl("sentinel"));
			RedisDriver rd = new RedisDriver("group1",EnvSetup.getSystemUrl("sentinel"));
			System.out.println(rd.getValue("SecStock:"+pia+":"+catalogId)+"**************************");
			System.out.println(rd.getHashAll("SecStock:"+pia+":"+catalogId)+"**************************");
			System.out.println(rd.getValueToMap("SecStock:"+pia+":"+catalogId)+"**************************");
			System.out.println(rd.getList("SecStock:"+pia+":"+catalogId)+"**************************");
			System.out.println(rd.getTtl("SecStock:"+pia+":"+catalogId)+"**************************");
			System.out.println("SecStock:"+pia+":"+catalogId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnfreezeAllocateStock_007() {
		Logger.start(true,"直播秒杀解冻冻结商品，不同规格，OperateType=2");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> catalogs = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			
			CreateLiveActivityBean createliveactivityBean = new CreateLiveActivityBean();
			LiveActivityDto LiveActivity=new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts=new ArrayList<>();
			ActivityProductDto activityProductDto1 = new ActivityProductDto();
			activityProductDto1.setProductId(productId);
			activityProductDto1.setActivityPrice(7d);
			activityProductDto1.setActivityStock(6);
			activityProductDto1.setCatalogId(catalogs.get(0));
			ActivityProductDto activityProductDto2 = new ActivityProductDto();
			activityProductDto2.setProductId(productId);
			activityProductDto2.setActivityPrice(7d);
			activityProductDto2.setActivityStock(6);
			activityProductDto2.setCatalogId(catalogs.get(1));
			ActivityProducts.add(activityProductDto1);
			ActivityProducts.add(activityProductDto2);
			
			LiveActivity.setActivityProducts(ActivityProducts);
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime()+1*60*1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			
			createliveactivityBean.getLiveActivity().setActivityProducts(ActivityProducts);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomWapper LiveRoomWapper = new LiveRoomWapper();
			Integer activityId = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			Integer LiveActivityProductId = LiveRoomWapper.selectLiveactivityproduct(activityId, LiveActivity.getActivityProducts().get(0).getProductId()).get(0).getLiveActivityProductId();
			//添加秒杀商品数据
			ProductManagerCallServiceV2.addSecKillStock(activityId, productId, sellerId, true);
			//初始化活动库存
			Integer anum1 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Integer anum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(1)).getIstocknum();
			//冻结库存
			String freezeId = "JD168188";
			Integer firstFreezeNum1 = 500;
			FreezeAllocateStockBean initFreezeAllocateStockBean1 = ProdStockCallService.initFreezeAllocateStockBean(activityId, Integer.valueOf(LiveActivityProductId), freezeId,productId, sellerId, catalogs.get(0), firstFreezeNum1);
			Integer firstFreezeNum2 = 600;
			FreezeAllocateStockBean initFreezeAllocateStockBean2 = ProdStockCallService.initFreezeAllocateStockBean(activityId, Integer.valueOf(LiveActivityProductId), freezeId,productId, sellerId, catalogs.get(1), firstFreezeNum2);
			ProdStockCallService.FreezeAllocateStock(initFreezeAllocateStockBean1);
			ProdStockCallService.FreezeAllocateStock(initFreezeAllocateStockBean2);
			
			unfreezeallocatestockBean.setActivityId(activityId);
			unfreezeallocatestockBean.setFreezeId(freezeId);
			unfreezeallocatestockBean.setIsFbxProduct(false);
			unfreezeallocatestockBean.setProductInActivityId(Integer.valueOf(LiveActivityProductId));
			unfreezeallocatestockBean.setProductID(productId);
			unfreezeallocatestockBean.setStoreType(-1);
			unfreezeallocatestockBean.setUserId(sellerId);
			unfreezeallocatestockBean.setOperateType(2);
			FreezeCatalogInfo freezeCatalogInfo1 = new FreezeCatalogInfo();
			freezeCatalogInfo1.setCatalogId(catalogs.get(0));
			freezeCatalogInfo1.setFreezeNum(100);
			freezeCatalogInfo1.setSku(null);
			FreezeCatalogInfo freezeCatalogInfo2 = new FreezeCatalogInfo();
			freezeCatalogInfo2.setCatalogId(catalogs.get(1));
			freezeCatalogInfo2.setFreezeNum(300);
			freezeCatalogInfo2.setSku(null);
			FreezeCatalogList.add(freezeCatalogInfo1);
			FreezeCatalogList.add(freezeCatalogInfo2);
			unfreezeallocatestockBean.setFreezeCatalogList(FreezeCatalogList);
			unfreezeallocatestockCall.setData(unfreezeallocatestockBean);
			unfreezeallocatestockCall.callService();
			
			String Code = unfreezeallocatestockCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			FreezeProductResult freezeProductResult1 = unfreezeallocatestockCall.getResults().get(0);
			Logger.verifyEquals(activityId, freezeProductResult1.getActivityId(), "验证活动编号");
			Logger.verifyEquals(productId, freezeProductResult1.getProductId(), "验证商品编号");
			Logger.verifyEquals(catalogs.get(0), freezeProductResult1.getCatalogId(), "验证规格编号");
			Logger.verifyEquals(freezeId, freezeProductResult1.getFreezeId(), "验证冻结编号");
			Logger.verifyEquals(LiveActivityProductId, freezeProductResult1.getProductInActivityId(), "验证商品在活动中的编号");
		
			FreezeProductResult freezeProductResult2 = unfreezeallocatestockCall.getResults().get(1);
			Logger.verifyEquals(activityId, freezeProductResult2.getActivityId(), "验证活动编号");
			Logger.verifyEquals(productId, freezeProductResult2.getProductId(), "验证商品编号");
			Logger.verifyEquals(catalogs.get(1), freezeProductResult2.getCatalogId(), "验证规格编号");
			Logger.verifyEquals(freezeId, freezeProductResult2.getFreezeId(), "验证冻结编号");
			Logger.verifyEquals(LiveActivityProductId, freezeProductResult2.getProductInActivityId(), "验证商品在活动中的编号");
		
			//冻结库存
			Integer freezeStockNum1 = new YmtProductFreezeStockWapper().selectByProductId(productId).get(0).getFreezeNum();
			Integer freezeStockNum2 = new YmtProductFreezeStockWapper().selectByProductId(productId).get(1).getFreezeNum();
			//规格库存
			Integer catalogNum1 = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(0)).getiNum();
			Integer catalogNum2 = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(1)).getiNum();
			//活动库存
			Integer activityknum1 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Integer activityknum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(1)).getIstocknum();
			
			Logger.verifyEquals(0,freezeStockNum1, "验证冻结表库存");
			Logger.verifyEquals(0,freezeStockNum2, "验证冻结表库存");
			
			Logger.verifyEquals(catalogNum-anum1-firstFreezeNum1, catalogNum1, "验证规格库存");
			Logger.verifyEquals(catalogNum-anum2-firstFreezeNum2, catalogNum2, "验证规格库存");
			
			Logger.verifyEquals(firstFreezeNum1+anum1, activityknum1, "验证活动库存");
			Logger.verifyEquals(firstFreezeNum2+anum2, activityknum2, "验证活动库存");
			
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			
			RedisDriver rd = new RedisDriver("group1",EnvSetup.getSystemUrl("sentinel"));
			System.out.println(rd.getValue("SecStock:"+LiveActivityProductId+":"+catalogs.get(0))+"**************************");
			Logger.verifyEquals(rd.getValue("SecStock:"+LiveActivityProductId+":"+catalogs.get(0)),activityknum1+"", "验证redis库存");
			long ttl=rd.getTtl("SecStock:"+LiveActivityProductId+":"+catalogs.get(0))*1000;
			Logger.verifyEquals((int)((ttl-endtime.getTime()+starttime.getTime())/1000/60/60/24), 29, "判断ttl自结束日期起向后延长30天");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnfreezeAllocateStock_008() {
		Logger.start(true,"活动秒杀解冻冻结商品，不同规格，验证redis");
		try {
			
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			//添加秒杀商品数据
			ProductManagerCallServiceV2.addSecKillStock(activityId,productId, sellerId, true);
			//初始化活动库存
			Integer anum1 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Integer anum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(1)).getIstocknum();
			//冻结库存
			String freezeId = "JD168188";
			Integer firstFreezeNum1 = 500;
			FreezeAllocateStockBean initFreezeAllocateStockBean1 = ProdStockCallService.initFreezeAllocateStockBean(activityId, Integer.valueOf(aids.get(0)), freezeId,productId, sellerId, catalogs.get(0), firstFreezeNum1);
			Integer firstFreezeNum2 = 600;
			FreezeAllocateStockBean initFreezeAllocateStockBean2 = ProdStockCallService.initFreezeAllocateStockBean(activityId, Integer.valueOf(aids.get(0)), freezeId,productId, sellerId, catalogs.get(1), firstFreezeNum2);
			ProdStockCallService.FreezeAllocateStock(initFreezeAllocateStockBean1);
			ProdStockCallService.FreezeAllocateStock(initFreezeAllocateStockBean2);
			
			unfreezeallocatestockBean.setActivityId(activityId);
			unfreezeallocatestockBean.setFreezeId(freezeId);
			unfreezeallocatestockBean.setIsFbxProduct(false);
			unfreezeallocatestockBean.setProductInActivityId(Integer.valueOf(aids.get(0)));
			unfreezeallocatestockBean.setProductID(productId);
			unfreezeallocatestockBean.setStoreType(-1);
			unfreezeallocatestockBean.setUserId(sellerId);
			unfreezeallocatestockBean.setOperateType(2);
			FreezeCatalogInfo freezeCatalogInfo1 = new FreezeCatalogInfo();
			freezeCatalogInfo1.setCatalogId(catalogs.get(0));
			freezeCatalogInfo1.setFreezeNum(100);
			freezeCatalogInfo1.setSku(null);
			FreezeCatalogInfo freezeCatalogInfo2 = new FreezeCatalogInfo();
			freezeCatalogInfo2.setCatalogId(catalogs.get(1));
			freezeCatalogInfo2.setFreezeNum(300);
			freezeCatalogInfo2.setSku(null);
			FreezeCatalogList.add(freezeCatalogInfo1);
			FreezeCatalogList.add(freezeCatalogInfo2);
			unfreezeallocatestockBean.setFreezeCatalogList(FreezeCatalogList);
			unfreezeallocatestockCall.setData(unfreezeallocatestockBean);
			unfreezeallocatestockCall.callService();
			
			String Code = unfreezeallocatestockCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			FreezeProductResult freezeProductResult1 = unfreezeallocatestockCall.getResults().get(0);
			Logger.verifyEquals(activityId, freezeProductResult1.getActivityId(), "验证活动编号");
			Logger.verifyEquals(productId, freezeProductResult1.getProductId(), "验证商品编号");
			Logger.verifyEquals(catalogs.get(0), freezeProductResult1.getCatalogId(), "验证规格编号");
			Logger.verifyEquals(freezeId, freezeProductResult1.getFreezeId(), "验证冻结编号");
			Logger.verifyEquals(aids.get(0), freezeProductResult1.getProductInActivityId()+"", "验证商品在活动中的编号");
		
			FreezeProductResult freezeProductResult2 = unfreezeallocatestockCall.getResults().get(1);
			Logger.verifyEquals(activityId, freezeProductResult2.getActivityId(), "验证活动编号");
			Logger.verifyEquals(productId, freezeProductResult2.getProductId(), "验证商品编号");
			Logger.verifyEquals(catalogs.get(1), freezeProductResult2.getCatalogId(), "验证规格编号");
			Logger.verifyEquals(freezeId, freezeProductResult2.getFreezeId(), "验证冻结编号");
			Logger.verifyEquals(aids.get(0), freezeProductResult2.getProductInActivityId()+"", "验证商品在活动中的编号");
		
			//冻结库存
			Integer freezeStockNum1 = new YmtProductFreezeStockWapper().selectByProductId(productId).get(0).getFreezeNum();
			Integer freezeStockNum2 = new YmtProductFreezeStockWapper().selectByProductId(productId).get(1).getFreezeNum();
			//规格库存
			Integer catalogNum1 = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(0)).getiNum();
			Integer catalogNum2 = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(1)).getiNum();
			//活动库存
			Integer activityknum1 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Integer activityknum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(1)).getIstocknum();
			
			Logger.verifyEquals(0,freezeStockNum1, "验证冻结表库存");
			Logger.verifyEquals(0,freezeStockNum2, "验证冻结表库存");
			
			Logger.verifyEquals(catalogNum-anum1-firstFreezeNum1, catalogNum1, "验证规格库存");
			Logger.verifyEquals(catalogNum-anum2-firstFreezeNum2, catalogNum2, "验证规格库存");
			
			Logger.verifyEquals(firstFreezeNum1+anum1, activityknum1, "验证活动库存");
			Logger.verifyEquals(firstFreezeNum2+anum2, activityknum2, "验证活动库存");
			
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			
			RedisDriver rd = new RedisDriver("group1",EnvSetup.getSystemUrl("sentinel"));
			String key="SecStock:"+aids.get(0)+":"+catalogs.get(0);
			Logger.comment("获取redis，key为："+key+"值为："+rd.getValue(key));
			Logger.verifyEquals(rd.getValue(key),activityknum1+"", "验证redis库存");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}